package com.steven.ourcontest.base

import android.app.Dialog
import android.content.Context
import android.util.ArrayMap
import android.view.View
import android.view.Window
import com.steven.ourcontest.R

class DialogBuilder @JvmOverloads constructor(
    private val context: Context,
    private val themeResId: Int = R.style.dialog
) {
    private var cancelable = false
    private var arrayListener = ArrayMap<Int,View.OnClickListener>()
    private var animation:Int?=null

    fun setCancelable(cancelable: Boolean): DialogBuilder {
        this.cancelable = cancelable
        return this
    }

    fun addListener(id: Int, viewListener: View.OnClickListener) : DialogBuilder{
        arrayListener[id] = viewListener
        return this
    }

    fun setAnim(themeResId: Int) : DialogBuilder{
        this.animation = themeResId
        return this
    }

    fun build(layoutId: Int): Dialog {
        val dialog = Dialog(context, themeResId)
        dialog.setCancelable(cancelable)
        dialog.setContentView(layoutId)
        animation?.let {
            val window: Window? = dialog.window
            window?.setWindowAnimations(it)
        }
        arrayListener.forEach {
            dialog.findViewById<View>(it.key).setOnClickListener(it.value)
        }
        arrayListener.clear()
        return dialog
    }

}